package cn.sylinx.hbatis.ext.starter.util;

import cn.sylinx.hbatis.db.common.FS;
import cn.sylinx.hbatis.ds.JdbcResourceManager;
import cn.sylinx.hbatis.ext.mirage.repository.DaoService;
import cn.sylinx.hbatis.ext.proxy.command.CommandRegistry;

public final class DaoHelper {

	public static <T> FS<T> createFS(Class<T> clazz) {
		return createFS(null, clazz);
	}

	public static <T> FS<T> createFS(String dsName, Class<T> clazz) {
		return create(dsName).createFS(clazz);
	}

	public static DaoService create() {
		return create(null);
	}

	public static DaoService create(String ds) {
		return DAO.create(ds);
	}

	/**
	 * 是否已初始化
	 * 
	 * @return
	 */
	public static boolean isInitialized() {

		String defaultJdbcResourceName = null;

		try {
			defaultJdbcResourceName = JdbcResourceManager.get().getDefaultJdbcResourceName();
		} catch (Exception e) {
			// do nothing
		}

		return defaultJdbcResourceName != null;
	}

	/**
	 * 获取代理对象
	 * 
	 * @param type
	 * @return
	 */
	public static <T> T getCommand(Class<T> type) {
		return CommandRegistry.INSTANCE.getCommand(type);
	}
}
